package com.game.server.message.proto.execute;

import com.comm.ISession;
import com.game.majiang.config.DataManager;
import com.game.majiang.config.LogManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.LogicTable;
import com.game.server.db.entity.XyUnionInfo;
import com.game.server.message.proto.in.AbstractEnterRoomReq;
import com.game.server.message.proto.out.EnterRoomRsp;
import com.game.server.system.AbstractRoom;

public class EnterRoomReq extends AbstractEnterRoomReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
				LogManager.getInstance().Log("进入房间client："+client);
        if (client != null&&client.checkClick(msgcode)) {

        	EnterRoomRsp rsp = new EnterRoomRsp();
        	rsp.setIsOK((byte) 1);

        	if(client.getTable() == null) {
        		rsp.setIsOK((byte) 4);
						AbstractRoom room = LogicServer.getInstance().getRoom(1);
						LogManager.getInstance().Log("进入房间号： "+this.roomNo);
						LogicTable table = (LogicTable) room.getTable(this.roomNo);

            	if(table != null) {
								LogManager.getInstance().Log("进入房间的俱乐部ID:"+table.getClubid());
            		if(table.getListKickPeopleUids().contains(client.getUserId())) {
            			rsp.setIsOK((byte) 5);
            			client.Send(rsp);
            			return ;
            		}
							  boolean nextFlg = false;
								if (table.getClubid()==0){
									nextFlg = true;  //不是俱乐部房间
								}else if (table.getClubid()>0){
									if (table.getUnionid()>0){
										XyUnionInfo unionInfo = DataManager.getInstance().getMapUnion().get(table.getUnionid());
										if (unionInfo!=null && unionInfo.getClubids().contains(Integer.valueOf(this.clubid))){
											nextFlg = false;
										}
									}else if (table.getClubid() == this.clubid){
										nextFlg = false;
									}
								}
									if (nextFlg){
										rsp.setIsOK((byte) 2);
										if(client.getVo().getIntegral() >= table.getCreate().getDifen() * 50) {

											LogManager.getInstance().Log("费用够了 table.getID()"+table.getID());
											LogManager.getInstance().Log("费用够了 table.gettype()"+table.gettype());
											LogManager.getInstance().Log("费用够了 this.clubid)"+this.clubid);
											LogManager.getInstance().Log("费用够了 client "+client);
											client.setTableid(table.getID());
											client.setRoomid(room.getID());// table.gettype());
											client.setClubid(this.clubid);
											table.enterWatchPlayer(client);
											rsp.setIsOK((byte) 0);
										}
									}else{
										//俱乐部联盟房间直接过
										client.setTableid(table.getID());
										client.setRoomid(room.getID());// table.gettype());
										client.setClubid(this.clubid);
										table.enterWatchPlayer(client);
										rsp.setIsOK((byte) 0);
									}
            	}
            }
        	client.Send(rsp);
        }
    }

}
